package com.aboo.infrastructure.generator;

import com.aboo.infrastructure.base.domain.po.BaseEntity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.builder.GeneratorBuilder;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/*
 * @description
 * @author Yata.Niko
 * @version 1.0
 * @date 2024/3/28
 */
public class CodeGenerator {

    public static void main(String[] args) {
        GlobalConfig config = GeneratorBuilder.globalConfigBuilder()
                .author("louan") // 作者
                .outputDir("F:\\java") // 生成路径
                .dateType(DateType.TIME_PACK)
                .commentDate("yyyy-MM-dd")
//                .enableSwagger()
                .build();//生成基本的SQL片段

        //mysql数据源配置
        DataSourceConfig dsc = new DataSourceConfig.Builder("jdbc:mysql://192.168.8.8:3306/demo_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&allowMultiQueries=true", "root", "root")
                .dbQuery(new MySqlQuery()).build();

        // 策略配置
        StrategyConfig strategy = GeneratorBuilder.strategyConfigBuilder()
                .addInclude("usr_user")    //表名
                .addTablePrefix("usr_")    //表前缀
                //Entity配置
                .entityBuilder()
                .enableFileOverride()
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel)
                .formatFileName("%sPo")
//                .versionColumnName("version")
//                .logicDeleteColumnName("del_flag")
//                .enableChainModel()//开启链式模型，默认值:false
                .enableLombok()//开启 lombok 模型，默认值:false
                .enableRemoveIsPrefix()//开启 Boolean 类型字段移除 is 前缀，默认值:false
                .enableTableFieldAnnotation()//开启生成实体时生成字段注解，默认值:false
//                .enableActiveRecord()//开启 ActiveRecord 模型，默认值:false
                .idType(IdType.AUTO)//全局主键类型
                .superClass(BaseEntity.class)
                .disableSerialVersionUID()
                .addSuperEntityColumns("create_user", "create_time", "update_user", "update_time", "del_flag")
                //Controller配置
                .controllerBuilder()
                .enableFileOverride()
                .enableHyphenStyle()
                .enableRestStyle()
                .formatFileName("%sController")
                //.superClass(BaseController.class)
                //Service配置
                .serviceBuilder()
                .enableFileOverride()
                .formatServiceFileName("%sService")
                .formatServiceImplFileName("%sServiceImpl")
                //Mapper配置
                .mapperBuilder()//mapper 策略配置
                .enableFileOverride()
                .enableBaseResultMap()//启用 BaseResultMap 生成，默认值:false
                .enableBaseColumnList()//启用 BaseColumnList，默认值:false
                .formatMapperFileName("%sMapper")//转换 mapper 接口文件名称后缀，mapper目录下的，例如：UserLoginMapper（有@Mapper）
                .formatXmlFileName("%sMapper")
                .build();

        String basePackage = "com.aboo";   //包名

        // 包名策略配置
        PackageConfig pkConfig = GeneratorBuilder.packageConfigBuilder().parent(basePackage)
                .mapper("mapper")
                .service("service")
                .controller("controller")
                .entity("model")
                .xml("mapper")
                //.pathInfo(Collections.singletonMap(OutputFile.mapperXml, "src/main/resources/mapper"))
                .build();

        TemplateConfig templateConfig = GeneratorBuilder.templateConfigBuilder().build();

        // 代码生成器
        AutoGenerator mpg = new AutoGenerator(dsc).global(config).strategy(strategy).template(templateConfig).packageInfo(pkConfig);
        mpg.execute();
    }

}
